package com.sunny.util;

import com.sunny.查询.Search;
import com.sunny.查询.二分查找.BinarySearch;

public class ArrayUtil {
    public static int[] instanceArray(int size) {
        int[] array = null;
        try{
            // 判断数组合法性
            if(size <=0) {
                throw new IllegalArgumentException();
            }

            array = new int[size];
            int length = size - 1;
            for(int i = 0; i < length; ++i) {
                array[i] = (int) (Math.random()*size);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return array;
    }

    public static void displayArray(int[] array, String sortName) {
        StringBuilder builder = new StringBuilder();
        int length = array.length;
        int lastIndex = length - 1;
        builder.append(sortName + " [");
        /*for(int i = 0; i < length; ++i) {
            if(i != lastIndex) {
                builder.append(array[i] + ",");
                continue;
            }
            builder.append(array[i]);
        }*/
        builder.append("]");
        System.out.println(builder.toString());
    }

    public static void displaySearchTime(int[] array, int key, String searchName, Search search) {
        StringBuilder builder = new StringBuilder();
        builder.append(searchName + " [耗时：");
        long startTime = System.currentTimeMillis();
        int high = array.length - 1;
        int index = search.search(array, key, 0, high);
        long endTime = System.currentTimeMillis();
        builder.append(endTime - startTime + " 索引：" + index + "]");
        System.out.println(builder);
    }
}
